package secretaryGUI;

import flexibility.userData;
import guiCode.secretaryScreenCode;

import java.awt.Color;
import java.awt.Point;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.table.JTableHeader;

import secretary.defineSemester;

import basic.ClientConsole;

import com.sun.rowset.CachedRowSetImpl;

public class handleLowerTableEvents {
	secretaryScreenCode ssc;
	secretaryAlerts sa = new secretaryAlerts();
	private userData classData = new userData();
	private int classID;
	private int studentID;
	private userData students = new userData();
	private String sender, recipient, content;
	private double recipientID;
	private int first = 1;
	updateUpperTableEntries UUTE = new updateUpperTableEntries();
	updateLowerTableEntries ULTE = new updateLowerTableEntries();
	defineSemester ds = new defineSemester();

	public void click(secretaryScreenCode code, createSecretaryWindow csw,
			Point point, ClientConsole secretaryConnection,
			CachedRowSetImpl crs, int semester) {
		// semester is the relevant semester we are working on
		ssc = code;
		csw.row = csw.table.rowAtPoint(point);
		csw.col = csw.table.columnAtPoint(point);

		if (ssc.getEditMode() == 14) {
			if (first == 0) {
				first = 1;
				// check if the student is registered already to the course
				students.deleteAll();
				students.addData(492);
				students.addData(1);
				students.addData(csw.table_1.getValueAt(
						csw.table_1.rowAtPoint(point), 0));
				students.addData(ds.identify(ssc.getSemesterForEdit(),
						secretaryConnection, crs));
				students.addData(csw.table.getValueAt(0, 0));
				secretaryConnection.acceptMsg(students);
				crs = secretaryConnection.getResults();
				try {
					if (crs.next() == true) {
						// student already registered to the course
						sa.showMessages(
								csw,
								code,
								csw.secretaryScreen,
								"Student already registered to this course!",
								"student manual registration to course - failure",
								1);
					} else {
						// check pre conditions
						students.deleteAll();
						students.addData(497);
						students.addData(1);
						students.addData(csw.table_1.getValueAt(
								csw.table_1.rowAtPoint(point), 0));
						secretaryConnection.acceptMsg(students);
						crs = secretaryConnection.getResults();
						if (crs.next() == true) {
							//course has pre condition
							//check if the student has this precondition
							students.deleteAll();
							students.addData(498);
							students.addData(1);
							students.addData(csw.table.getValueAt(
									0, 0));
							students.addData(crs.getString(1));
							students.addData(ds.identify(
									ssc.getSemesterForEdit(),
									secretaryConnection, crs));
							
							secretaryConnection.acceptMsg(students);
							crs = secretaryConnection.getResults();
							if (crs.next() == false) {
								//student is missing the pre condition
								sa.showMessages(
										csw,
										code,
										csw.secretaryScreen,
										"Student is missing pre conditions for this course!",
										"Manual registration to course - failure",
										1);
							} else {
								students.deleteAll();
								students.addData(493);
								students.addData(0);
								students.addData(csw.table_1.getValueAt(
										csw.table_1.rowAtPoint(point), 0));
								students.addData(ds.identify(
										ssc.getSemesterForEdit(),
										secretaryConnection, crs));
								students.addData(csw.table.getValueAt(0, 0));
								secretaryConnection.acceptMsg(students);
								crs = secretaryConnection.getResults();
								sa.showMessages(
										csw,
										code,
										csw.secretaryScreen,
										"Registered the student to the course",
										"student manual registration to course - success",
										4);
							}

						} else {
							students.deleteAll();
							students.addData(493);
							students.addData(0);
							students.addData(csw.table_1.getValueAt(
									csw.table_1.rowAtPoint(point), 0));
							students.addData(ds.identify(
									ssc.getSemesterForEdit(),
									secretaryConnection, crs));
							students.addData(csw.table.getValueAt(0, 0));
							secretaryConnection.acceptMsg(students);
							crs = secretaryConnection.getResults();
							sa.showMessages(
									csw,
									code,
									csw.secretaryScreen,
									"Registered the student to the course",
									"student manual registration to course - success",
									4);
						}

					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				first = 0;
			}

		}

		if (ssc.getEditMode() == 13) {
			students.deleteAll();
			students.addData(489);
			students.addData(1);
			students.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 0));
			secretaryConnection.acceptMsg(students);
			crs = secretaryConnection.getResults();
			try {
				crs.next();
				content = "Request approval to remove "
						+ csw.table_1.getValueAt(csw.table_1.rowAtPoint(point),
								1).toString() + " from course "
						+ crs.getString(1);// studentID
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			students.deleteAll();
			students.addData(490);
			students.addData(1);
			students.addData(ssc.getMyID());
			students.addData(content);
			secretaryConnection.acceptMsg(students);
			crs = secretaryConnection.getResults();
			try {
				if (crs.next() == false) {
					// msg like t hat does not exists
					sa.showMessages(csw, ssc, csw.secretaryScreen,
							"Sent request to manager",
							"Manual removal of student", 4);
					// get the sender

					students.deleteAll();
					students.addData(487);
					students.addData(1);
					students.addData(ssc.getMyID());
					secretaryConnection.acceptMsg(students);
					crs = secretaryConnection.getResults();
					try {
						crs.next();
						sender = crs.getString(1);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// get the recipient
					students.deleteAll();
					students.addData(488);
					students.addData(1);
					secretaryConnection.acceptMsg(students);
					crs = secretaryConnection.getResults();
					try {
						crs.next();
						recipient = crs.getString(1) + " " + crs.getString(2);
						recipientID = crs.getDouble(3);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					students.deleteAll();
					students.addData(489);
					students.addData(1);
					students.addData(csw.table_1.getValueAt(
							csw.table_1.rowAtPoint(point), 0));
					secretaryConnection.acceptMsg(students);
					crs = secretaryConnection.getResults();
					try {
						crs.next();
						content = "Request approval to remove "
								+ csw.table_1.getValueAt(
										csw.table_1.rowAtPoint(point), 1)
										.toString() + " from course "
								+ crs.getString(1);// studentID
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Calendar reportTime = Calendar.getInstance();
					SimpleDateFormat formatter = new SimpleDateFormat(
							"yyyy/MM/dd");
					students.deleteAll();
					students.addData(486);
					students.addData(0);
					students.addData(recipientID);
					students.addData(ssc.getMyID());
					students.addData(1);
					students.addData(formatter.format(reportTime.getTime()));
					students.addData(sender);
					students.addData(content);
					students.addData(recipient);
					secretaryConnection.acceptMsg(students);
					crs = secretaryConnection.getResults();
				} else {
					if (crs.getString(9).equals("-1")) {
						sa.showMessages(csw, code, csw.secretaryScreen,
								"Still waiting for manager approval",
								"Student manual removal from course", 4);
					}
					// request already exists
					if (crs.getString(9).equals("0")) {
						sa.showMessages(csw, code, csw.secretaryScreen,
								"Manager didnt approve the request!",
								"Student manual removal from course", 1);
					}
					if (crs.getString(9).equals("1")) {
						sa.showMessages(csw, code, csw.secretaryScreen,
								"Student removed from course!",
								"Student manual removal from course", 4);
						students.deleteAll();
						students.addData(491);
						students.addData(0);
						students.addData(csw.table_1.getValueAt(
								csw.table_1.rowAtPoint(point), 1));
						students.addData(csw.table_1.getValueAt(
								csw.table_1.rowAtPoint(point), 0));
						students.addData(csw.table_1.getValueAt(
								csw.table_1.rowAtPoint(point), 2));
						secretaryConnection.acceptMsg(students);
						crs = secretaryConnection.getResults();
						// update the display
						students.deleteAll();
						students.addData(420);
						students.addData(1);
						students.addData(csw.table.getValueAt(
								csw.table.rowAtPoint(point), 0));// this
																	// is
																	// the
																	// student
																	// id
						students.addData(ds.identify(ssc.getSemesterForEdit(),
								secretaryConnection, crs));// this is the
															// semester
						secretaryConnection.acceptMsg(students);
						crs = secretaryConnection.getResults();
						// this will show the student's courses
						ULTE.update(csw, crs);
					}

				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			// this is the manual removal of a student from course

		}

		if (ssc.getEditMode() == 12) {
			// this is the remove course from teacher mode
			// show the teacher courses
			JTableHeader header = csw.table.getTableHeader();
			header.setBackground(Color.GREEN);
			JTableHeader header1 = csw.table_1.getTableHeader();
			header1.setBackground(Color.RED);
			classData.deleteAll();
			classData.addData(406);
			classData.addData(1);
			classData.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 0));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			ULTE.update(csw, crs);
			classData.deleteAll();
			classData.addData(415);
			classData.addData(1);
			classData.addData(ssc.getTeacherID());
			classData.addData(ds.identify(ssc.getSemesterForEdit(),
					secretaryConnection, crs));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			UUTE.update(csw, crs);

		}

		if (ssc.getEditMode() == 11) {
			// this is to add courses to teacher
			JTableHeader header = csw.table.getTableHeader();
			header.setBackground(Color.GREEN);
			JTableHeader header1 = csw.table_1.getTableHeader();
			header1.setBackground(Color.RED);
			// this will show the teacher courses
			classData.deleteAll();
			classData.addData(407);
			classData.addData(1);
			classData.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 0));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			ULTE.update(csw, crs);
		}

		if (ssc.getEditMode() == 8) {
			// this will show the student's courses (manual assign)
			JTableHeader header = csw.table.getTableHeader();
			header.setBackground(Color.RED);
			JTableHeader header1 = csw.table_1.getTableHeader();
			header1.setBackground(Color.GREEN);
			classData.deleteAll();
			classData.addData(252);
			classData.addData(1);
			classData.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 0));
			classData.addData(ds.identify(ssc.getSemesterForEdit(),
					secretaryConnection, crs));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			UUTE.update(csw, crs);
			csw.lblDOWN.setText("These are the courses in this semester:");
			csw.lblDOWN
					.setText("These are the courses the student is registered in this semester:");
			classData.deleteAll();
			classData.addData(419);
			classData.addData(1);
			classData.addData(ds.identify(ssc.getSemesterForEdit(),
					secretaryConnection, crs));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			ULTE.update(csw, crs);
			first = 1;
			ssc.setEditMode(14);

		}

		if (ssc.getEditMode() == 7) {
			// this will delete course from chose class
			classData.deleteAll();
			classData.addData(249);
			classData.addData(0);
			classData.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 0));
			classData.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 1));
			secretaryConnection.acceptMsg(classData);
			secretaryConnection.getResults();
			// this will delete students that were assigne to course
			// automatically
			// first select the student sn the class
			classData.deleteAll();
			classData.addData(484);
			classData.addData(1);
			classData.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 0));
			classData.addData(ds.identify(ssc.getSemesterForEdit(),
					secretaryConnection, crs));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			try {
				if (crs.next() == true) {
					// class has students
					crs.beforeFirst();
					students.deleteAll();
					while (crs.next()) {
						students.addData(crs.getString(1));
					}
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			for (int i = 0; i < students.getLength(); i++) {
				classData.deleteAll();
				classData.addData(250);
				classData.addData(0);
				classData.addData(students.getData(i));// studentID
				classData.addData(csw.table_1.getValueAt(
						csw.table_1.rowAtPoint(point), 1));// courseID
				classData.addData(ds.identify(ssc.getSemesterForEdit(),
						secretaryConnection, crs));// semesterID
				classData.addData(csw.table_1.getValueAt(
						csw.table_1.rowAtPoint(point), 0));// classID
				secretaryConnection.acceptMsg(classData);
				secretaryConnection.getResults();
			}

			// this will update the table
			classData.deleteAll();
			classData.addData(248);
			classData.addData(1);
			classData.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 0));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			ULTE.update(csw, crs);
			ssc.viewAssignedClasses();
		}
		if (ssc.getEditMode() == 5) {
			/* this will bring the student back to the class */
			classData.deleteAll();
			classData.addData(90);
			classData.addData(0);
			semester = Integer.parseInt(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 4).toString());
			classID = Integer.parseInt(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 3).toString());
			classData.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 3));
			classData.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 4));
			classData.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 0));
			secretaryConnection.acceptMsg(classData);
			secretaryConnection.getResults();
			// this will add the student to the class in students table
			classData.deleteAll();
			classData.addData(167);
			classData.addData(0);
			classData.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 0));
			classData.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 3));
			classData.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 4));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			/* this will delete the student from the lower table */
			classData.deleteAll();
			classData.addData(94);
			classData.addData(0);
			classData.addData(csw.table_1.getValueAt(
					csw.table_1.rowAtPoint(point), 0));
			secretaryConnection.acceptMsg(classData);
			secretaryConnection.getResults();
			/* this will update the displays */
			classData.deleteAll();
			classData.addData(87);
			classData.addData(1);
			classData.addData(semester);
			classData.addData(classID);
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			UUTE.update(csw, crs);
			classData.deleteAll();
			classData.addData(93);
			classData.addData(1);
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			ULTE.update(csw, crs);
		}
		if (ssc.getEditMode() == 3) {
			// this is for edit class mode
			if (csw.col == 1) {
				csw.alertsLbl.setText("This field is not editable!");
				csw.alertsLbl.setForeground(Color.RED);
			}
			if (csw.col == 2) {
				csw.alertsLbl.setForeground(Color.BLUE);
				csw.alertsLbl
						.setText("Click on classID field to add/remove students from class. Click on className to change the class name");
				sa.showMessages(csw, ssc, csw.secretaryScreen, "", "", 5);
			}
			if (csw.col == 0) {
				csw.alertsLbl
						.setText("Click on 'Add students to class' to add more students to this class. Click on 'Remove student from class' to remove students from this class");
				ssc.setEditMode(2);
				csw.btnAddStudentTo.setEnabled(true);
				csw.btnRemoveStudentFrom.setEnabled(true);
				csw.alertsLbl.setForeground(Color.BLUE);

				classData.deleteAll();
				classID = Integer.parseInt((csw.table.getValueAt(csw.row,
						csw.col).toString()));// this will get the classID
				if (semester == 1) {// editing next semester
					classData.addData(15);
					classData.addData(1);// this is for read
					secretaryConnection.acceptMsg(classData);
					crs = secretaryConnection.getResults();
					try {
						crs.next();
						classData.deleteAll();
						classData.addData(87);
						classData.addData(1);// this is for read
						classData.addData(crs.getString(1));
						classData.addData(classID);
						secretaryConnection.acceptMsg(classData);
						crs = secretaryConnection.getResults();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if (semester == 0) {// editing current semester
					classData.addData(82);
					classData.addData(1);// this is for read
					secretaryConnection.acceptMsg(classData);
					crs = secretaryConnection.getResults();
					try {
						crs.next();
						classData.deleteAll();
						classData.addData(87);
						classData.addData(1);// this is for read
						classData.addData(crs.getString(1));
						classData.addData(classID);
						secretaryConnection.acceptMsg(classData);
						crs = secretaryConnection.getResults();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				try {
					if (crs.next() == false) {// check if class has already
												// students in it
						csw.btnRemoveStudentFrom.setEnabled(false);
						crs.beforeFirst();
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ULTE.update(csw, crs);

			}
		}
	}
}
